<?php
/**
 * ${NAMESPACE}\DefaultController
 *
 * @author Robin Fai <robinfai9@gmail.com>
 * @link http://widget.robinfai.com
 * @license http://www.yiiframework.com/license/
 * @version $Id: $ 15/1/20 20:11 robin.fai $
 *
 */

/**
 * Class HomeController
 * @property ProfilerModule $module
 */
class HomeController extends CController {

    public function init(){
        if(!Yii::app()->hasComponent('profiler')
            || !in_array(Yii::app()->user->name,Yii::app()->getComponent('profiler')->whiteList)
        ){
            throw new CHttpException(403);
        }
    }
    /**
     * 性能日志查询列表
     */
    public function actionIndex() {
        $log = new ProfilingLog;
        $log->attributes = Yii::app()->request->getParam('ProfilingLog');

        $this->render('index', array('log' => $log,));

    }

    /**
     * 性能日志详情
     */
    public function actionDetail() {
        $id = Yii::app()->request->getParam('id');
        $log = ProfilingLog::model()->findByPk($id);
        /* @var $log ProfilingLog */
        $this->render('detail', array('log' => $log));
    }

    /**
     * 性能标签内代码预览
     * @param $file
     * @param $beginLine
     * @param $endLine
     */
    public function actionGetFileContent($file, $beginLine, $endLine) {
        $file = file(Yii::app()->basePath . $file);
        $content = '';
        if (($beginLine - 3) > 0) {
            $beginLine -= 3;
        }

        if ((count($file) - $endLine) > 3) {
            $endLine += 3;
        } else {
            $endLine += (count($file) - $endLine);
        }
        for ($i = $beginLine; $i <= $endLine; $i++) {
            $content .= $file[$i - 1] . "\n";
        }
        echo json_encode(array('content' => $content));
    }
} 